package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.ColumnActivity;
import com.runen.infinitemovement.model.entity.InformationListEntity;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class NewConsultAdapter extends RecyclerView.Adapter<NewConsultAdapter.MyViewHolder> {
    private Context context;
    private List<InformationListEntity> mDatas;
    private OnClick onClick;

    public NewConsultAdapter(Context context) {
        this.context = context;
        this.mDatas = new ArrayList<>();
    }

    public void setData(List<InformationListEntity> mDatas){
        if (mDatas != null) {
            this.mDatas.clear();
            this.mDatas.addAll(mDatas);
            notifyDataSetChanged();
        }
    }

    public void addList(List<InformationListEntity> mDatas) {
        if (mDatas != null) {
            this.mDatas.addAll(mDatas);
            notifyItemRangeInserted(this.mDatas.size() - mDatas.size(), mDatas.size());
        }
    }

    public List<InformationListEntity> getData() {
        return mDatas;
    }

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_consult, null, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if (holder == null) return;
        InformationListEntity item = mDatas.get(position);
        holder.tv_company_activity.setText(item.title);
        holder.tv_see_num.setText(item.readNumber + "浏览");
        holder.tv_comment_num.setText(item.commentNumber + "评论");
        GlideUtils.loadImage(context, item.cover + "?x-oss-process=image/resize,m_fill,w_144,h_144", holder.rci_cover);
        holder.tv_consult_click.setText(item.subjectName);
        if (item.recommend == 1) {
            holder.tv_hot.setVisibility(View.VISIBLE);
        }
        holder.tv_consult_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ColumnActivity.class);
                intent.putExtra("subjectId", item.subjectId);
                context.startActivity(intent);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null)
                    onClick.OnItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_consult_click;
        TextView tv_company_activity;
        TextView tv_see_num;
        TextView tv_comment_num;
        TextView tv_hot;
        RoundedCornersImage rci_cover;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_consult_click = itemView.findViewById(R.id.tv_consult_click);
            tv_company_activity = itemView.findViewById(R.id.tv_company_activity);
            tv_see_num = itemView.findViewById(R.id.tv_see_num);
            tv_comment_num = itemView.findViewById(R.id.tv_comment_num);
            tv_hot = itemView.findViewById(R.id.tv_hot);
            rci_cover = itemView.findViewById(R.id.rci_cover);
        }
    }

    public interface OnClick {
        void OnItemClick(int position);
    }
}
